﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGLib
{
    //Key und Value dürfen jeweils nur einmal vorhanden sein!!
    public class XMLDictionary<k, v>
    {
        private List<XMLDictPair<k, v>> items;

        public XMLDictionary()
        {
            this.items = new List<XMLDictPair<k, v>>();
        }

        public bool Add(XMLDictPair<k, v> item)
        {
            foreach (XMLDictPair<k, v> pair in items)
            {
                if (pair.Key.Equals(item.Key)) { return false; }
                if (pair.Value.Equals(item.Value)) { return false; }
            }
            items.Add(item);
            return true;
        }

        public bool Add(k key, v value)
        {
            foreach (XMLDictPair<k, v> pair in items)
            {
                if (pair.Key.Equals(key)) { return false; }
                if (pair.Value.Equals(value)) { return false; }
            }
            items.Add(new XMLDictPair<k, v>(key, value));
            return true;
        }

        public void Remove(XMLDictPair<k, v> item)
        {
            items.Remove(item);
        }

        public XMLDictPair<k, v> GetItem(k key)
        {
            foreach (XMLDictPair<k, v> pair in this.items)
            {
                if (pair.Key.Equals(key)) //Oder == ?
                {
                    return pair;
                }
            }
            return null;
        }

        public XMLDictPair<k, v> GetItem(v value)
        {
            foreach (XMLDictPair<k, v> pair in this.items)
            {
                if (pair.Value.Equals(value)) //Oder == ?
                {
                    return pair;
                }
            }
            return null;
        }

        //Zum Auslesen für XML-Serialisierung (Schreibgeschützt)
        public List<XMLDictPair<k, v>> Items
        {
            get { return this.items; }
        }
    }

    public class XMLDictPair<k, v>
    {
        private k key;
        private v value;

        public XMLDictPair(k key, v value)
        {
            this.key = key;
            this.value = value;
        }

        //Parameterloser Konstruktor für XML-Serialisierung
        public XMLDictPair() { }

        public k Key
        {
            get { return this.key; }
            set { this.key = value; }
        }

        public v Value
        {
            get { return this.value; }
            set { this.value = value; }
        }
    }

    public class MapArray
    {
        private int[][] _base, _transition, _object, _walkable;

        public int[][] BaseLayer
        {
            get { return this._base; }
            set { this._base = value; }
        }

        public int[][] TransitionLayer
        {
            get { return this._transition; }
            set { this._transition = value; }
        }

        public int[][] ObjectLayer
        {
            get { return this._object; }
            set { this._object = value; }
        }

        public int[][] WalkableLayer
        {
            get { return this._walkable; }
            set { this._walkable = value; }
        }

        public MapArray() { }

        public MapArray(int width, int height)
        {
            this.Initialize(width, height);
        }

        public void Initialize(int width, int height)
        {
            _base = new int[width][];
            _transition = new int[width][];
            _object = new int[width][];
            _walkable = new int[width][];

            for (int i = 0; i != width; i++)
            {
                _base[i] = new int[height];
                _transition[i] = new int[height];
                _object[i] = new int[height];
                _walkable[i] = new int[height];
            }
        }

        public int GetTile(int x, int y, int layer)
        {
            switch (layer)
            {
                case 0:
                    return _base[x][y];
                case 1:
                    return _transition[x][y];
                case 2:
                    return _object[x][y];
                case 3:
                    return _walkable[x][y];
            }
            return -1;
        }

        public void SetTile(int x, int y, int layer, int tile)
        {
            switch (layer)
            {
                case 0:
                    _base[x][y] = tile;
                    break;
                case 1:
                    _transition[x][y] = tile;
                    break;
                case 2:
                    _object[x][y] = tile;
                    break;
                case 3:
                    _walkable[x][y] = tile;
                    break;
            }
        }
    }

    public enum Direction
    {
        Up, Down, Left, Right, None
    }
}
